#include "StkItem.h"
#include "Tools.h"
#include "OptCtrlTools.h"
#include "OptUiTools.h"

void __cdecl Deler(void * ptrVal)
{
	vector<string> * vt = nullptr;
	if ((vt = reinterpret_cast<vector<string> *>(ptrVal)) == nullptr) return;
	delete vt;
	vt = nullptr;	
}

const bool CStkItem::Init(const void * pvdPara, char * pszMsgTxt, int iMsgTxtSize)
{
	m_bInitFlg = false;
	string strVal("");
	IValueSet * ptrValSet = reinterpret_cast<IValueSet *>(const_cast<void *>(pvdPara));
	m_ptrContractsByCode = &ValueSet().List("ContractsByCode");
	m_ptrContractsByNum  = &ValueSet().List("ContractsByNum");	
	m_ptrCalculateQueue = &OptCtrlTools().StkTrdCtrl().ValueSet().Queue(CALCULATE_QUEUE_NAME);
	if (strVal == MONITOR_CODE)
	{
		ValueSet().SetValue("OPT_UNDL_CODE", "-1");
		ValueSet().SetValue("KEY", MONITOR_CODE);
		ValueSet().SetValue("OPT_UNDL_CODES", "-1");
		ValueSet().SetValue("NEW_THIRD_MARKET_INDEX", 0);
		ValueSet().SetValue("MAKE_MARKTE_INDEX", 0);
		ValueSet().SetValue("SYS", 17);
		ValueSet().SetValue("ITF_ZSDM", "");
		ValueSet().SetValue("ASSET_ID", "");
		ValueSet().SetValue("PostFlg", "0");
		ValueSet().SetValue("BIZ_STATUS", "");
		ValueSet().SetValue("QY_SUM", 0);
		ValueSet().SetValue("MARGIN_INCL_DYN", 0);
		ValueSet().SetValue("HQ_TIME", 0);
		ValueSet().SetValue("HQ_TIME_FLG", true);
		ValueSet().SetValue("HQ_TIME_START", 0);
		ValueSet().Table("VIEW_TRD");
		ValueSet().Table("VIEW_ENT");
	}
	else
	{
		if (!ptrValSet) goto __end;
		if (!ptrValSet->GetValue("OPT_UNDL_CODE", strVal)) goto __end;
		ValueSet().Assign(ptrValSet);
		ValueSet().SetValue("SORT_TYPE", "A");
		ValueSet().SetValue("InitContractFlg", false);
		ValueSet().SetValue("InitOptFlg", false);
		ValueSet().SetValue("SUM1", 0);
		ValueSet().SetValue("SUM2", 0);
		ValueSet().SetValue("RISK_NUM", 0);
		ValueSet().SetValue("CUST_FLG", false);
		ValueSet().Table("Tb_Ent");
		ValueSet().Table("VIEW_SUB_RISK");
		ValueSet().Table("VIEW_OPT_RISK");
		ValueSet().Table("VIEW_ENT");
		ValueSet().Table("VIEW_TRD");
	}
	m_bInitFlg = true;
__end:
	return m_bInitFlg;
}

const bool CStkItem::AddJson(IPacket * ptrResult)
{
	bool bRetVal = false;
	IValueSet * ptrValList = nullptr;
	string strName(""), strVal(""), strKey;
	if (FrameTools().StrFun().Pos(ptrResult->LBM_NO(), "CGT_") >= 0) goto __end;	
	strName = "C_" + string(ptrResult->LBM_NO()) + string("__JsonResult_HIS");
	const char * pszRetVal = ptrResult->AsString(RESULT_SET_NAME);
	if (FrameTools().StrFun().Pos("MARKET1|CONFIRM15|MATCH15", ptrResult->LBM_NO()) < 0)
	{
		ValueSet().GetValue("KEY", strKey);
		strKey += "|" + string(ptrResult->LBM_NO());
		ptrValList = &ValueSet();
	}		
	else
	{
		IContract * ptrContract = nullptr;
	    auto ptrRecInfo = ptrResult->ReadRecord(RESULT_SET_NAME, 1);
		if (strcmp(ptrResult->LBM_NO(), "MARKET1") == 0)
		{
			const char * pszOptNum = ptrResult->GetFieldValueAsString(ptrRecInfo, "OPT_NUM");			
			ptrContract = OptCtrlTools().StkTrdCtrl().GetContractByOptNum(pszOptNum);
		}
		else
		{
			const char * pszOptCode = ptrResult->GetFieldValueAsString(ptrRecInfo, "OPT_CODE");
			IContract * ptrContract = OptCtrlTools().StkTrdCtrl().GetContractByOptCode(pszOptCode);			
		}
		if (ptrContract != nullptr)
		{
			strKey = ptrContract->GetOptCode();
			strKey += "|" + string(ptrResult->LBM_NO());
			ptrValList = &ptrContract->ValueSet();
		}
			
	}//CALCULATE_QUEUE_NAME
	if (ptrValList == nullptr) goto __end;
	ptrValList->GetValue(strName.c_str(), strVal);
	if (FrameTools().StrFun().Pos("L3030101|L3030102|L3030303|CONFIRM15|MATCH15|MARKET1", ptrResult->LBM_NO()) < 0 && strcmp(pszRetVal, strVal.c_str()) == 0) goto __end;
	ptrValList->SetValue(strName.c_str(), string(pszRetVal));
	strName = "C_" + string(ptrResult->LBM_NO()) + string("__JsonResult");
	ptrValList->SetValue(strName.c_str(), string(ptrResult->AsString()));
	strName = "C_" + string(ptrResult->LBM_NO()) + "_Empty";
	ptrValList->SetValue(strName.c_str(), FrameTools().StrFun().Pos(ptrResult->LBM_NO(), "L3030101|L3030102|") < 0 ? ptrResult->RECORD_COUNT(RESULT_SET_NAME) <= 0 : false);
	m_ptrCalculateQueue->Push(strKey);
	bRetVal = true;
__end:
	return bRetVal;
}

bool CStkItem::GetWarningFlg()
{
	bool bRetVal = false;
	ValueSet().GetValue("WarningFlg", bRetVal);
	return bRetVal;
}

void CStkItem::SetWarningFlg(const bool bValue)
{
	ValueSet().CompareExchange("WarningFlg", bValue);
}

bool CStkItem::GetErrorFlg()
{
	bool bRetVal = false;
	ValueSet().GetValue("ErrorFlg", bRetVal);
	return bRetVal;
}

void CStkItem::SetErrorFlg(const bool bValue)
{
	ValueSet().CompareExchange("ErrorFlg", bValue);
}

bool CStkItem::GetHQ_STATUS(string & strValue)
{
	return ValueSet().GetValue("HQ_STATUS", strValue);
}

void CStkItem::SetHQ_STATUS(const string & strValue)
{	
	ValueSet().CompareExchange("HQ_STATUS", strValue);
}

string CStkItem::GetKey(string & strValue)
{
	strValue = "";
	ValueSet().GetValue("KEY", strValue);
    return strValue;
}

void CStkItem::SetKey(const string & strKey)
{
	ValueSet().CompareExchange("KEY", strKey);
}

bool CStkItem::GetStopBusinessFlg()
{
	bool bRetVal = false;
	int iHqStatus = -1;
	if (ValueSet().GetValue("HQ_STATUS", iHqStatus))
	{
		if (iHqStatus == 0) return bRetVal;
		if (iHqStatus == 2) return bRetVal;
	    bRetVal = true;
	}
	return bRetVal;
}

bool CStkItem::GetMsg(string & strValue)
{
	return GetFormMsg(MONITOR_CODE, strValue);
}

bool CStkItem::GetFormMsg(const char * pszName, string & strValue)
{
	strValue = "";
	IValue * ptrVal = nullptr;
	if (!ValueSet().List("MSG_TXT").Queue(pszName).Pop(&ptrVal)) goto __end;
	ptrVal->GetValue(strValue);
	delete ptrVal;
	ptrVal = nullptr;
__end:
	return strValue.length() != 0;
}

void * CStkItem::GetWindowsPosInfo()
{
	void * ptrRetVal = nullptr;
	ValueSet().GetValue("WindowsPosInfo", &ptrRetVal);
	return ptrRetVal;
}

void CStkItem::SetWindowsPosInfo(void * ptrVal)
{
	ValueSet().SetValue("WindowsPosInfo", ptrVal, nullptr);
}

bool CStkItem::GetNormList(const char * pszName, void * ptrList)
{
	bool bRetVal = false;
	void * ptrVal = nullptr;
	vector<string> * ptrDes = nullptr, * ptrSou = nullptr;
	if (FrameTools().StrFun().IsEmpty(pszName)) goto __end;
	if (!ValueSet().List("NormList").ExistValue(pszName)) goto __end;
	if ((ptrDes = reinterpret_cast<vector<string> *>(ptrList)) == nullptr) goto __end;
	if (!ValueSet().List("NormList").GetValue(pszName, &ptrVal)) goto __end;
	if ((ptrSou = reinterpret_cast<vector<string> *>(ptrVal)) == nullptr) goto __end;
	ptrDes->clear();
	ptrDes->assign(ptrSou->begin(), ptrSou->end());
	bRetVal = ptrDes->size();
__end:
	return bRetVal;
}

bool CStkItem::SetNormList(const char * pszName, void * ptrList)
{
	bool bRetVal = false;
	vector<string> * ptrDes = nullptr, *ptrSou = nullptr;
	if (FrameTools().StrFun().IsEmpty(pszName)) goto __end;
	if ((ptrSou = reinterpret_cast<vector<string> *>(ptrList)) == nullptr) goto __end;
	if (ValueSet().List("NormList").ExistValue(pszName)) ValueSet().List("NormList").DelVar(pszName);
	ptrDes = new vector<string>;
	ptrDes->assign(ptrSou->begin(), ptrSou->end());
	sort(ptrDes->begin(), ptrDes->end());
	bRetVal = ValueSet().List("NormList").SetValue(pszName, reinterpret_cast<void *>(ptrDes), Deler);
__end:
	return bRetVal;
}

unsigned int CStkItem::GetContractCount()
{
	return m_ptrContractsByCode->ItemCount();
}

IContract * CStkItem::GetContractByOptCode(const char * pszOptCode)
{
	return GetContract(m_ptrContractsByCode, pszOptCode);
}

void CStkItem::SetContractByOptCode(const char * pszOptCode, IContract * ptrContract)
{
	SetContract(m_ptrContractsByCode, pszOptCode, ptrContract);
}

IContract * CStkItem::GetContractByOptNum(const char * pszOptNum)
{	
	return GetContract(m_ptrContractsByNum, pszOptNum);
}

void CStkItem::SetContractByOptNum(const char * pszOptNum, IContract * ptrContract)
{
	SetContract(m_ptrContractsByNum, pszOptNum, ptrContract);
	
}

bool CStkItem::GetContractInfo(void * vtContractInfo, int iType)
{	
	return iType == 0 ? m_ptrContractsByCode->GetValNameList(vtContractInfo) : m_ptrContractsByNum->GetValNameList(vtContractInfo);
}

IContract * CStkItem::GetContract(IValueSet * pszList, const char * pszCode)
{	
	IBaseObject * ptrVal = nullptr;
	IContract * ptrRetVal = nullptr;
	if (!pszList->GetValue(pszCode, &ptrVal)) goto __end;
	ptrRetVal = dynamic_cast<IContract *>(ptrVal);
__end:
	return ptrRetVal;
}

void CStkItem::SetContract(IValueSet * pszList, const char * pszCode, IContract * ptrContract)
{
	pszList->SetValue(pszCode, ptrContract, false);
}

string CStkItem::OPT_UNDL_CODE(string & strValue)
{
	strValue = "";
	ValueSet().GetValue("OPT_UNDL_CODE", strValue);
	return strValue;
}


